package com.sunreal.paygateway.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sunreal.paygateway.common.AssertUtil;
import com.sunreal.paygateway.common.MySeq;
import com.sunreal.paygateway.common.PayConstant;
import com.sunreal.paygateway.dao.model.MchInfo;
import com.sunreal.paygateway.dao.model.PayOrder;
import com.sunreal.paygateway.dao.vo.SunrealPayOrder;
import com.sunreal.paygateway.service.BaseService;
import com.sunreal.paygateway.service.IPayChannel4AliService;
import com.sunreal.paygateway.service.IPayChannel4WxService;
import com.sunreal.paygateway.service.SunrealPayOrderService;
import com.sunreal.paygateway.sys.exception.BusinessException;
import com.sunreal.paygateway.sys.exception.WebErrCode;
import com.sunreal.paygateway.sys.util.GsonUtil;

/**
 * 功能描述：聚合支付统一下单Service实现类
 *
 * @Author 彭广
 * @Date 2019/4/16
 */
@Service
public class SunrealPayOrderServiceImpl extends BaseService implements SunrealPayOrderService {

    @Autowired
    private IPayChannel4WxService wxPayService;

    @Autowired
    private IPayChannel4AliService aliPayService;

    /**
     * 创建订单，这里没有调用微信或者支付宝的创建订单接口
     *
     * @param sunrealPayOrder 订单详情
     * @return 订单ID
     */
    @Override
    public String createPayOrder(SunrealPayOrder sunrealPayOrder) throws BusinessException {
        PayOrder payOrder = GsonUtil.fromJson(GsonUtil.toJson(sunrealPayOrder), PayOrder.class);
        payOrder.setPayorderid(MySeq.getPay(payOrder.getMchid()));
        PayOrder existsOrder = super.baseSelectPayOrderByMchIdAndMchOrderNo(payOrder.getMchid(), payOrder.getMchorderno());
        if (existsOrder != null) {
            throw new BusinessException(WebErrCode.PAYORDER_EXISTS);
        }
        int i = super.baseCreatePayOrder(payOrder);
        if (i != 1) {
            throw new BusinessException(WebErrCode.CREATE_PAYORDER_FAILED);
        }
        PayOrder createdOrder = super.baseSelectPayOrderByMchIdAndMchOrderNo(payOrder.getMchid(), payOrder.getMchorderno());
        AssertUtil.notBlank(createdOrder, WebErrCode.CREATE_PAYORDER_FAILED.getErroMsg());
        return createdOrder.getPayorderid();
    }

    /**
     * 根据支付中心的订单ID，打开订单支付页面（这里调用了微信或支付宝的下单接口）
     *
     * @param payorderid 定单ID
     * @param resKey     需要返回的商户秘钥
     * @return 根据蚂蚁金服和微信支付返回不同内容，但是都以页面形式展现
     */
    @Override
    public Map<String, Object> payOrder(String payorderid, StringBuffer resKey) throws BusinessException {
        try {
            PayOrder order = super.baseSelectPayOrder(payorderid);
            AssertUtil.notBlank(order, "没有找到对应的订单信息！");
            String channelId = order.getChannelid();
            AssertUtil.notBlank(channelId, "订单支付渠道为空，无法继续支付！");
            MchInfo mchInfo = super.baseSelectMchInfo(order.getMchid());
            resKey.append(mchInfo.getReskey());
            Map<String, Object> result = new HashMap<>();
            switch (order.getChannelname()) {
                case PayConstant.CHANNEL_NAME_WX:
                    result = wxPayService.doWxPayReq(order);
                    break;
                case PayConstant.CHANNEL_NAME_ALIPAY:
                    result = aliPayService.doAliPayReq(order);
                    break;
                default:
                    break;
            }
            return result;
        } catch (Exception e) {
            throw new BusinessException(WebErrCode.OPEN_ORDER_PAGE_FAILED);
        }
    }


}
